//
//  LSDrawerController.m
//  LSDrawerExample
//
//  Created by LuckyStrike on 2018/3/26.
//  Copyright © 2018年 LuckyStrike. All rights reserved.
//

#import "LSDrawerController.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "DrawerViewController.h"
#import "LSContainerView.h"

#define SCREEN_WIDTH    ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT   ([UIScreen mainScreen].bounds.size.height)

#define VIEW_BOUNDS     self.view.bounds

/**
 加载策略：
 
 1.赋值子控制器集合的setter方法加载drawerVC MiddleVC和Bottom控制器
 2.准备展开抽屉前加载
 
    选取第一个
 */

/**
 视图区分
 
 1.创建时
 
 */



CGFloat const LSDrawerDefaultWidth = 280.0f;
CGFloat const LSDrawerDefaultAnimationVelocity = 840.0f;

NSTimeInterval const LSDrawerDefaultFullAnimationDelay = 0.10f;

CGFloat const MMDrawerDefaultBounceDistance = 50.0f;

NSTimeInterval const MMDrawerDefaultBounceAnimationDuration = 0.2f;
CGFloat const MMDrawerDefaultSecondBounceDistancePercentage = .25f;

CGFloat const MMDrawerDefaultShadowRadius = 10.0f;
CGFloat const MMDrawerDefaultShadowOpacity = 0.8;

NSTimeInterval const MMDrawerMinimumAnimationDuration = 0.15f;

CGFloat const MMDrawerBezelRange = 20.0f;

CGFloat const MMDrawerPanVelocityXAnimationThreshold = 200.0f;

/** The amount of overshoot that is panned linearly. The remaining percentage nonlinearly asymptotes to the max percentage. */
CGFloat const MMDrawerOvershootLinearRangePercentage = 0.75f;

/** The percent of the possible overshoot width to use as the actual overshoot percentage. */
CGFloat const MMDrawerOvershootPercentage = 0.1f;


typedef NS_ENUM(NSInteger,LSContainerSubviewTag) {
    LSContainerSubviewTagTop = 99,
    LSContainerSubviewTagMiddle,
    LSContainerSubviewTagBottom,
    
//    LSContainerSubviewTagDrawer,
    
    LSContainerSubviewTagReserve,
};

@interface LSDrawerController ()<UIGestureRecognizerDelegate>

/**   */
@property (nonatomic,assign) BOOL isOpening;
/**   */
@property (nonatomic,assign) BOOL isAnimating;
/**   */
//@property (nonatomic,strong) LSContainerView *containerView;

/**   */
@property (nonatomic,strong) LSContainerView *topContainerView;
/**   */
@property (nonatomic,strong) LSContainerView *middleContainerView;
/**   */
@property (nonatomic,strong) LSContainerView *bottomContainerView;
/**   */
@property (nonatomic,strong) LSContainerView *reserveContainerView;

/**   */
@property (nonatomic,assign) CGFloat ghostingScale;
/**   */
@property (nonatomic,assign) CGFloat ghostingCornerRadius;
/**   */
@property (nonatomic,assign) CGFloat ghostingTotalMargin;
/**   */
@property (nonatomic,assign) CGPoint startingPanPoint;

@end

@implementation LSDrawerController
//TODO:selectedIndex 做越界判断
- (void)setIsOpening:(BOOL)isOpening{
    _isOpening = isOpening;
    _topContainerView.isOpening = isOpening;
    _middleContainerView.isOpening = isOpening;
    _bottomContainerView.isOpening = isOpening;
}

- (void)setIsAnimating:(BOOL)isAnimating {
    _isAnimating = isAnimating;
//    self.view.userInteractionEnabled = !isAnimating;
}

- (UIViewController *)selectedViewContoller {
    return self.contentControllers[self.selectedIndex];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self defaultSetup];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self defaultSetup];
    }
    return self;
}

- (void)defaultSetup {
    
    [self setDrawerWidth:280.0f];
    [self setAnimationVelocity:840.0f];
    [self setPanVelocityXAnimationThreshold:200.0f];
    
    [self setContentScale:0.8f];
    [self setGhostingScale:0.95f];
    [self setGhostingCornerRadius:5.0f];
    [self setGhostingTotalMargin:12.0f];
    [self setBezelPanningCenterViewRange:30.0f];
    
    [self setIsOpening:NO];
    
    [self setContentShadowRadiusRange:LSMakeRange(0, 5.0f)];
    [self setContentShadowOpacityRange:LSMakeRange(0, 0.25f)];
}

- (void)setContentControllers:(NSArray<UIViewController *> *)contentControllers {
    NSParameterAssert(contentControllers && contentControllers.count);
    if ([_contentControllers isEqual:contentControllers]) {
        return;
    }
    //移除
    for (UIViewController *vc in _contentControllers) {
        [vc willMoveToParentViewController:nil];
        [vc beginAppearanceTransition:NO animated:NO];
        [vc.view removeFromSuperview];
        [vc endAppearanceTransition];
        [vc removeFromParentViewController];
    }
    _contentControllers = contentControllers;
    //初始化时暂时初始化首页，其他控制器等准备跳转时添加
    _topContainerView = (LSContainerView*)[self loadContainerOrDrawerWithTag:LSContainerSubviewTagTop view:_topContainerView];
    _middleContainerView = (LSContainerView*)[self loadContainerOrDrawerWithTag:LSContainerSubviewTagMiddle view:_middleContainerView];
    _bottomContainerView = (LSContainerView*)[self loadContainerOrDrawerWithTag:LSContainerSubviewTagBottom view:_bottomContainerView];
}

- (void)setDrawerController:(UIViewController *)drawerController {
    NSParameterAssert(drawerController);
    if (drawerController == _drawerController) {
        return;
    }
    //移除
    if (_drawerController != nil) {
        [_drawerController willMoveToParentViewController:nil];
        [_drawerController.view removeFromSuperview];
        [_drawerController removeFromParentViewController];
    }
    _drawerController = drawerController;
    [self addChildViewController:_drawerController];
    [_drawerController beginAppearanceTransition:YES animated:NO];
    [self.view addSubview:_drawerController.view];
    [self.view sendSubviewToBack:_drawerController.view];
    _drawerController.view.frame = VIEW_BOUNDS;
    [_drawerController endAppearanceTransition];
    [_drawerController didMoveToParentViewController:self];
}

#pragma mark - Open/Close methods
-(void)toggleDrawerAnimated:(BOOL)animated completion:(void(^)(BOOL finished))completion {
    if (self.isOpening) {
        [self closeDrawerAnimated:animated completion:completion];
    }else {
        [self openDrawerAnimated:animated completion:completion];
    }
}

-(void)closeDrawerAnimated:(BOOL)animated completion:(void(^)(BOOL finished))completion {
    if (self.isAnimating || !self.isOpening) {
        if (completion) {
            completion(NO);
        }
    }else {
        self.isAnimating = YES;
        
        NSTimeInterval duration = MAX(self.drawerWidth/self.animationVelocity, MMDrawerMinimumAnimationDuration);
        //9.动画
        __weak typeof(self) weakSelf = self;
        [UIView animateWithDuration:(animated?duration:0.0) delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            
            [weakSelf updateAnimationForPan:0.0f];
            
        } completion:^(BOOL finished) {
            
            
            //            [[weakSelf getContentControllerWithMask:ContainerSubviewMaskTop] endAppearanceTransition];
            //            [[weakSelf getContentControllerWithMask:ContainerSubviewMaskMiddle] endAppearanceTransition];
            //            [[weakSelf getContentControllerWithMask:ContainerSubviewMaskBottom] endAppearanceTransition];
            weakSelf.isOpening = NO;
            weakSelf.isAnimating = NO;
            if (completion) {
                completion(finished);
            }
            
        }];
        //10.动画完成，跳转页生命周期调用，设置开启状态、动画状态，完成block调用
        
        //11.页面完成状态设置
    }
}

-(void)openDrawerAnimated:(BOOL)animated completion:(void(^)(BOOL finished))completion {
    if (self.isAnimating || self.isOpening) {
        if (completion) {
            completion(NO);
        }
    }else {
        //2.设置开始动画
        self.isAnimating = YES;

        //4.跳转页恢复默认
        
        //5.跳转页设置frame
        
        //6.动画初始状态调用
        [self prepareToOpenDrawer];
        //7.跳转页生命周期调用
//        [[self getContentControllerWithMask:ContainerSubviewMaskTop] beginAppearanceTransition:YES animated:animated];
//        [[self getContentControllerWithMask:ContainerSubviewMaskMiddle] beginAppearanceTransition:YES animated:animated];
//        [[self getContentControllerWithMask:ContainerSubviewMaskBottom] beginAppearanceTransition:YES animated:animated];
        //8.计算速度时间
        NSTimeInterval duration = MAX(self.drawerWidth/self.animationVelocity, MMDrawerMinimumAnimationDuration);
        //9.动画
        __weak typeof(self) weakSelf = self;
        [UIView animateWithDuration:(animated?duration:0.0) delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            
            [weakSelf updateAnimationForPan:1.0f];

        } completion:^(BOOL finished) {
            
            
//            [[weakSelf getContentControllerWithMask:ContainerSubviewMaskTop] endAppearanceTransition];
//            [[weakSelf getContentControllerWithMask:ContainerSubviewMaskMiddle] endAppearanceTransition];
//            [[weakSelf getContentControllerWithMask:ContainerSubviewMaskBottom] endAppearanceTransition];
            weakSelf.isOpening = YES;
            weakSelf.isAnimating = NO;
            if (completion) {
                completion(finished);
            }
            
        }];
        //10.动画完成，跳转页生命周期调用，设置开启状态、动画状态，完成block调用
        
        //11.页面完成状态设置
        
    }
}

- (void)exchangeSelectedIndex:(NSInteger)index animated:(BOOL)animated completion:(void(^)(BOOL finished))completion {
    //1.判断开关状态
    //1.1 抽屉开启时
    
    //1.2.1 点击当前控制器，直接关闭
    
    //1.2.2 点击中层或下层，创建新图层，第一层或前两层向右移除
    
    //
    
    
    
    
    //1.2 抽屉关闭时
    
    
    
    
    
    
}

- (void)exchangeSelectedItem:(NSInteger)newIndex from:(NSInteger)oldIndex{
    //1.设置selectedIndex
    
    //2.给当前内容页置ContainerSubviewMaskRemove，并动画向右移出屏幕
    
    //3.新建第四层内容页，替换顶部内容页，并关闭抽屉
    
    //4.设置新的中下内容页
}

#pragma mark -- 抽屉动画

//- (void)setupContainerSubview:(ContainerSubviewMask)mask drawerPercentage:(CGFloat)percentage{
//    UIView *tempView = nil;
//    if ([self.containerView viewWithTag:mask] == nil) {
//        tempView = [[UIView alloc] initWithFrame:self.containerView.bounds];
//        tempView.backgroundColor = [UIColor clearColor];
//        //标记
//        tempView.tag = mask;
//        //圆角
////        tempView.layer.masksToBounds = YES;
//        tempView.layer.cornerRadius = self.ghostingCornerRadius;
//        [self.containerView addSubview:tempView];
//        //如果有控制器添加控制器
//        UIViewController *tempVC = [self getContentControllerWithMask:mask];
//        if (tempVC) {
//            tempVC.view.layer.masksToBounds = YES;
//            tempVC.view.layer.cornerRadius = self.ghostingCornerRadius;
//            [tempView addSubview:tempVC.view];
//            tempVC.view.frame = tempView.bounds;
//        }
//    }else {
//        tempView = [self.containerView viewWithTag:mask];
//    }
//
//    //阴影
//    tempView.layer.shadowRadius = [self animationValueFromRange:self.contentShadowRadiusRange drawerPercentage:percentage];
//    tempView.layer.shadowColor = [UIColor blackColor].CGColor;
//    tempView.layer.shadowOpacity = [self animationValueFromRange:self.contentShadowOpacityRange drawerPercentage:percentage];
//    tempView.layer.shadowOffset = CGSizeMake(-3, 3);
//
//    /**
//     *  透明度和偏移量随百分比进行调整
//     */
//    switch (mask) {
//        case ContainerSubviewMaskTop:
//        {
//            NSLog(@"ContainerSubviewMaskTop:%@",NSStringFromCGRect(tempView.frame));
//            [self.containerView bringSubviewToFront:tempView];
//            //透明度 1 - 0.9 偏移量 ghostingTotalMargin * 0
//            tempView.alpha = 1.0f - (1.0f - 0.9f) * percentage;
//
//        }
//            break;
//        case ContainerSubviewMaskMiddle:
//        {
//            [self.containerView sendSubviewToBack:tempView];
//            tempView.alpha = 1.0f - (1.0f - 0.7f)*percentage;
//            CGRect tempRect = self.containerView.bounds;
//
//            NSLog(@"ContainerSubviewMaskMiddle:%@",NSStringFromCGRect(tempView.frame));
//
////
//            tempRect.origin.y = tempRect.size.height * (1.0f - self.ghostingScale) * percentage / 2;
//            tempRect.origin.x -= self.ghostingTotalMargin * percentage;
//            tempRect.size.width = tempRect.size.width * (1.0f - (1.0f - self.ghostingScale) * percentage);
//            tempRect.size.height = tempRect.size.height * (1.0f - (1.0f - self.ghostingScale) * percentage);
//
//            tempView.frame = tempRect;
//
//        }
//            break;
//        case ContainerSubviewMaskBottom:
//        {
//            NSLog(@"ContainerSubviewMaskBottom:%@",NSStringFromCGRect(tempView.frame));
//            [self.containerView sendSubviewToBack:tempView];
//            tempView.alpha = 1.0f - (1.0f - 0.4f)*percentage;
//            CGRect tempRect = self.containerView.bounds;
//
//            tempRect.origin.x -= self.ghostingTotalMargin * 2 * percentage;
//            tempRect.origin.y = tempRect.size.height * (1.0f - self.ghostingScale * self.ghostingScale)*percentage / 2;
//            tempRect.size.width = tempRect.size.width * (1.0f - (1.0f - self.ghostingScale * self.ghostingScale)*percentage);
//            tempRect.size.height = tempRect.size.height * (1.0f - (1.0f - self.ghostingScale * self.ghostingScale)*percentage);
//
//
//            tempView.frame = tempRect;
//        }
//            break;
//        default:
//            break;
//    }
//}

- (void)prepareToOpenDrawer {
    //层级调整
    [self.view sendSubviewToBack:self.drawerController.view];
}
/**
 *  1.抽屉页暂时移除
    2.虚影移除
    3.内容页交互开启
    4.参数重置
 */
- (void)resetAfterCloseDrawer {}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self defaultSetup];
    [self setupGestureRecognizers];
}

- (void)updateAnimationForPan:(CGFloat)percent {
    
    [self updateContainerAnimation:LSContainerSubviewTagTop percent:percent];
    [self updateContainerAnimation:LSContainerSubviewTagMiddle percent:percent];
    [self updateContainerAnimation:LSContainerSubviewTagBottom percent:percent];
//    [self setupContainerSubview:ContainerSubviewMaskTop drawerPercentage:percent];
//    [self setupContainerSubview:ContainerSubviewMaskMiddle drawerPercentage:percent];
//    [self setupContainerSubview:ContainerSubviewMaskBottom drawerPercentage:percent];
    
//    CGFloat leftWidth = self.drawerWidth * percent;
//    CGFloat containerScale = 1 - (1 - self.contentScale) * percent;
//
//    CATransform3D transform = CATransform3DIdentity;
//    transform = CATransform3DMakeScale(containerScale, containerScale, 1.0f);
//    transform = CATransform3DTranslate(transform, leftWidth, 0, 0);
    
//    self.containerView.layer.transform = transform;
//    NSLog(@"%s,containerView : %@",__func__,NSStringFromCGRect(self.containerView.frame));
}

#pragma mark -- GestureRecognizers
- (void)setupGestureRecognizers {
    UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureCallback:)];
    [pan setDelegate:self];
    [self.view addGestureRecognizer:pan];
    
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureCallback:)];
    [tap setDelegate:self];
    [self.view addGestureRecognizer:tap];
}

-(void)panGestureCallback:(UIPanGestureRecognizer *)panGesture {
    
    switch (panGesture.state) {
        case UIGestureRecognizerStateBegan:
        {
            /**
             *  开始滑动各种情况
                1.关闭、无动画
                2.开启、无动画
                3.动画中（偏移量决定偏向关闭或开启）
                4.多指触碰：忽略
             */
            if (self.isAnimating) {
                [panGesture setEnabled:NO];
                
            }else {
                //关闭无动画状态进行滑动准备：prepareToOpenDrawer
                if (self.isOpening == NO) {
                    [self prepareToOpenDrawer];
                }else{//开启无动画
                }
                self.isAnimating = YES;
//                self.startingPanPoint = self.containerView.frame.origin;
                self.startingPanPoint = self.topContainerView.frame.origin;

            }
        }
            break;
        case UIGestureRecognizerStateChanged:
        {
            /**
             *  起点记录点位，点位差值/区间
             正值--向右移动-- 大于区间一半移动动画完全开启抽屉  小于一半回复关闭状态 reset
             
             max 不大于1.0
             */
            CGPoint translatedPoint = [panGesture translationInView:/*self.containerView*/self.topContainerView];
            CGFloat offsetX = [self roundedOriginXForDrawerConstriants:self.startingPanPoint.x + translatedPoint.x];

            
            //两种状态忽略，开启时右划，关闭时左滑
            if ((self.isOpening && translatedPoint.x > 0)||(!self.isOpening && translatedPoint.x < 0)) {
                return;
            }
            [self updateAnimationForPan:ABS(offsetX/self.drawerWidth)];
            
            
            
            if (ABS(offsetX/self.drawerWidth) > 1.0f && ABS(offsetX/self.drawerWidth) < 0) {
                NSLog(@"%f *** ABS:%f",offsetX/self.drawerWidth,ABS(offsetX/self.drawerWidth));
            }
            
            break;
        }
        case UIGestureRecognizerStateEnded:
        case UIGestureRecognizerStateCancelled:
        {
            CGPoint translatedPoint = [panGesture translationInView:/*self.containerView*/self.topContainerView];
            CGFloat offsetX = [self roundedOriginXForDrawerConstriants:self.startingPanPoint.x + translatedPoint.x];
            
            __weak typeof(self) weakSelf = self;
            
            NSTimeInterval duration;
            if (ABS(offsetX/self.drawerWidth) < 0.5f) {
                duration = ABS(self.topContainerView.frame.origin.x)/self.panVelocityXAnimationThreshold;
            }else {
                duration = ABS(self.topContainerView.frame.origin.x - self.drawerWidth)/self.panVelocityXAnimationThreshold;
            }
            
            [UIView animateWithDuration:duration delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
                
                if (ABS(offsetX/self.drawerWidth) < 0.5f) {
                    weakSelf.isOpening = NO;
                    [weakSelf updateAnimationForPan:0];
                }else {
                    weakSelf.isOpening = YES;
                    [weakSelf updateAnimationForPan:1.0f];
                }
                
            } completion:^(BOOL finished) {
                [panGesture setEnabled:YES];
                weakSelf.isAnimating = NO;
            }];
        }
            
            break;
        default:
            break;
    }
    
    
}

- (void)tapGestureCallback:(UITapGestureRecognizer *)tapGesture {
    [self closeDrawerAnimated:YES completion:nil];
}

#pragma mark -- helper
- (CGFloat)animationValueFromRange:(LSRange)range drawerPercentage:(CGFloat)percentage{
    if (range.endValue > range.startValue) {
        return range.startValue + (range.endValue - range.startValue) *percentage;
    }else if (range.endValue < range.startValue) {
        return range.startValue - (range.startValue - range.endValue) * percentage;
    }else {
        return range.startValue;
    }
}

//用于处理弹簧效果的线性函数
- (CGFloat)roundedOriginXForDrawerConstriants:(CGFloat)offsetX {
    return offsetX;
}




#pragma mark -- 视图区分
//按顺序获取内容页,有可能为空
- (UIViewController*)getContentControllerWithTag:(LSContainerSubviewTag)tag {
    switch (tag) {
        case LSContainerSubviewTagTop:
        case LSContainerSubviewTagReserve:
            return self.contentControllers[self.selectedIndex];
            break;
        case LSContainerSubviewTagMiddle:
        {
            if (self.contentControllers.count < 2) {
                return nil;
            }else if (self.selectedIndex + 1 >= self.contentControllers.count) {
                return self.contentControllers[0];
            }else {
                return self.contentControllers[self.selectedIndex + 1];
            }
        }
            break;
        case LSContainerSubviewTagBottom:
        {
            if (self.contentControllers.count < 3) {
                return nil;
            }else if (self.selectedIndex + 2 >= self.contentControllers.count) {
                return self.contentControllers[0];
            }else {
                return self.contentControllers[self.selectedIndex + 2];
            }
        }
            break;
        default:
            return nil;
            break;
    }
}

- (UIView*)getContainerViewWithTag:(LSContainerSubviewTag)tag {
    switch (tag) {
        case LSContainerSubviewTagTop:
            return self.topContainerView;
            break;
        case LSContainerSubviewTagMiddle:
            return self.middleContainerView;
            break;
        case LSContainerSubviewTagBottom:
            return self.bottomContainerView;
            break;
        case LSContainerSubviewTagReserve:
            return self.reserveContainerView;
            break;
        default:
            break;
    }
}
- (UIView*)loadContainerOrDrawerWithTag:(LSContainerSubviewTag)tag view:(UIView*)tempView{
    
    UIViewController *tempVC = [self getContentControllerWithTag:tag];
    if (!tempView) {
        tempView = [[LSContainerView alloc] initWithFrame:VIEW_BOUNDS];
        tempView.backgroundColor = [UIColor blueColor];
        tempView.layer.cornerRadius = _ghostingCornerRadius;
        [self.view addSubview:tempView];
    }
    
    if (!tempVC) {
        tempView.backgroundColor = [UIColor whiteColor];
    }else if (!tempView.subviews.count || ![tempView.subviews.firstObject isEqual:tempVC.view]){//如果没有子视图或者视图与切换后的子视图不符
        //移除之前的图层 //TODO:移除子控制器
        [tempView.subviews.firstObject removeFromSuperview];
        
        [self addChildViewController:tempVC];
        tempVC.view.frame = VIEW_BOUNDS;
        [tempView addSubview:tempVC.view];
        [tempVC beginAppearanceTransition:YES animated:NO];
        tempVC.view.layer.masksToBounds = YES;
        tempVC.view.layer.cornerRadius = _ghostingCornerRadius;
        [tempVC endAppearanceTransition];
        [tempVC didMoveToParentViewController:self];
    }

    switch (tag) {
        case LSContainerSubviewTagTop:
            break;
        case LSContainerSubviewTagMiddle:
        case LSContainerSubviewTagBottom:
        case LSContainerSubviewTagReserve:
            //重新添加子控制器才要调整层级
            [self.view sendSubviewToBack:tempView];
            break;
        default:
            break;
    }
    return tempView;
}

- (CATransform3D)transformWithTag:(LSContainerSubviewTag)tag percent:(CGFloat)percent {
    
    CATransform3D transform = CATransform3DIdentity;
    CGFloat drawerWidth = 0.0,containerScale = 0.0;
    switch (tag) {
        case LSContainerSubviewTagTop:
        {
            drawerWidth = self.drawerWidth * percent;
            containerScale = [self animationValueFromRange:LSMakeRange(1.0f, self.contentScale) drawerPercentage:percent];
        }
            break;
        case LSContainerSubviewTagMiddle:
        {
            drawerWidth = [self animationValueFromRange:LSMakeRange(0, self.drawerWidth - self.ghostingTotalMargin) drawerPercentage:percent];
            containerScale = [self animationValueFromRange:LSMakeRange(1.0f, self.contentScale * self.ghostingScale) drawerPercentage:percent];
        }
            break;
        case LSContainerSubviewTagBottom:
        {
            drawerWidth = [self animationValueFromRange:LSMakeRange(0, self.drawerWidth - self.ghostingTotalMargin * 2) drawerPercentage:percent];
            containerScale = [self animationValueFromRange:LSMakeRange(1.0f, self.contentScale * self.ghostingScale * self.ghostingScale) drawerPercentage:percent];
        }
            break;
        case LSContainerSubviewTagReserve:
        {
            drawerWidth = [self animationValueFromRange:LSMakeRange(0, self.drawerWidth - self.ghostingTotalMargin * 3) drawerPercentage:percent];
            containerScale = [self animationValueFromRange:LSMakeRange(1.0f, self.contentScale * self.ghostingScale * self.ghostingScale * self.ghostingScale) drawerPercentage:percent];
        }
            break;
        default:
            break;
    }
    transform = CATransform3DMakeScale(containerScale, containerScale, 1.0f);
    transform = CATransform3DTranslate(transform, drawerWidth, 0, 0);
    return transform;
}

- (void)updateContainerAnimation:(LSContainerSubviewTag)tag percent:(CGFloat)percentage{
    
    LSContainerView *tempView = (LSContainerView*)[self getContainerViewWithTag:tag];
    //阴影 TODO:可以添加到创建视图方法里
    tempView.layer.shadowColor = [UIColor blackColor].CGColor;
    tempView.layer.shadowOffset = CGSizeMake(-3, 3);
    
    tempView.layer.shadowRadius = [self animationValueFromRange:self.contentShadowRadiusRange drawerPercentage:percentage];
    tempView.layer.shadowOpacity = [self animationValueFromRange:self.contentShadowOpacityRange drawerPercentage:percentage];
    /**
     *  透明度和偏移量随百分比进行调整
     TODO:可能需要层级调整
     [self.view bringSubviewToFront:tempView];
     [self.view sendSubviewToBack:tempView];
     */
    
    LSRange range = LSMakeRange(1.0f, 1.0f);;
    
    switch (tag) {
        case LSContainerSubviewTagTop:
            range = LSMakeRange(1.0f, 0.9f);
            break;
        case LSContainerSubviewTagMiddle:
            range = LSMakeRange(1.0f, 0.7f);
            break;
        case LSContainerSubviewTagBottom:
            range = LSMakeRange(1.0f, 0.4f);
            break;
        case LSContainerSubviewTagReserve:
            range = LSMakeRange(1.0f, 0.2f);
            break;
        default:
            break;
    }
    
    tempView.alpha = [self animationValueFromRange:range drawerPercentage:percentage];
    tempView.layer.transform = [self transformWithTag:tag percent:percentage];
}

#pragma mark -- UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    //TODO:抽屉关闭期间、不在内容视图范围内、单击手势，返回NO
    if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]] && [self.view.gestureRecognizers containsObject:gestureRecognizer]) {
        if (self.isOpening) {
            CGPoint point = [touch locationInView:self.view];
            if (CGRectContainsPoint(self.topContainerView.frame, point) || CGRectContainsPoint(self.middleContainerView.frame, point) || CGRectContainsPoint(self.bottomContainerView.frame, point)) {
                return YES;
            }
        }
        return NO;
    }else {
        return YES;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
